/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	QueryDef.java
 * 模块说明：	
 * 修改历史：
 * May 14, 2007 - zhangyanbo - 创建。
 */
package com.hd123.rumba.qt.def;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.hd123.rumba.cartridge.ICartFunctionView;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.qt.QTConstants;
import com.hd123.rumba.runtime.BaseEntity;
import com.hd123.rumba.user.IPermission;

/**
 * @author zhangyanbo
 * 
 */
public class QueryDef extends BaseEntity implements IQueryDef {

  /** 属性：查询定义名 */
  private String name;
  /** 属性：相对于QTConstants.QDHOME的路径 */
  private String location;
  /** 属性：不带路径的文件名 */
  private String filename;
  /** 属性：关联权限 */
  private IPermission permission;
  /** 属性：相关功能视图 */
  private List<IQueryDefFuncView> fucvViews = new ArrayList<IQueryDefFuncView>();

  public IPermission getPermission() throws BusinessException {
    return permission;
  }

  public void setPermission(IPermission perm) throws BusinessException {
    this.permission = perm;
  }

  public String getLocation() throws BusinessException {
    if (location == null || location.trim().length() == 0)
      return "/";
    else
      return location;
  }

  public void setLocation(String location) throws BusinessException {
    if (location != null && location.trim().length() > 0) {
      if (location.charAt(0) != '/')
        this.location = "/" + location;
      else
        this.location = location;
    } else
      this.location = "/";
  }

  public String getName() throws BusinessException {
    return name;
  }

  public void setName(String name) throws BusinessException {
    this.name = name;
  }

  public String getFilename() throws BusinessException {
    return filename;
  }

  public void setFilename(String filename) throws BusinessException {
    this.filename = filename;
  }

  public List<IQueryDefFuncView> getFucvViews() throws BusinessException {
    return fucvViews;
  }

  public void setFucvViews(List<IQueryDefFuncView> fucvViews) throws BusinessException {
    this.fucvViews = fucvViews;
  }

  public String getFullFilename() throws BusinessException {
    if ("/".equals(getLocation()))
      return QTConstants.QDHOME + "/" + filename;
    else
      return QTConstants.QDHOME + getLocation() + "/" + filename;
  }

  public String getRelativeFilename() throws BusinessException {
    if ("/".equals(getLocation()))
      return "/" + filename;
    else
      return getLocation() + "/" + filename;
  }

  public File getFile() throws BusinessException {
    return new File(getFullFilename());
  }

  public File getTemplateFile() throws BusinessException {
    String s = getFullFilename();
    return new File(s.toLowerCase().replace(".sql", ".fr3"));
  }

  public boolean atRoot() throws BusinessException {
    return "/".equals(getLocation());
  }

  public String getPermissionName() throws BusinessException {
    return "查询报表." + getRelativeFilename().toUpperCase();
  }

  public String getCFVPermissionName(ICartFunctionView cfv) throws BusinessException {
    return cfv.getPrefix() + "." + getPermissionName();
  }
}
